package com.dw.auth.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.dw.apis.UserClient;
import com.dw.auth.service.AuthUserService;
import com.dw.common.entitys.BaseResponse;
import com.dw.common.enums.ErrorCode;
import com.dw.common.utils.ParseClientResultUtils;
import com.dw.common.utils.ThrowUtils;
import com.dw.model.auth.dtos.AuthUserDTO;
import com.dw.model.auth.vos.AuthUserVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service("authUserService")
public class AuthUserServiceImpl implements AuthUserService {

    @Resource
    private UserClient userClient;

    @Override
    public AuthUserVO login(AuthUserDTO authUserDTO) {
        // 调用feign获取用户id
        BaseResponse<Long> leiId = userClient.login(authUserDTO);
        // 校验feign返回的用户id
        ParseClientResultUtils.parseClientResult(leiId);
        // 登录
        StpUtil.login(leiId.getData());
        // 构建返回对象
        AuthUserVO authUserVO = new AuthUserVO();
        String tokenValue = StpUtil.getTokenValue();
        authUserVO.setToken(tokenValue);
        authUserVO.setLeiId(String.valueOf(leiId.getData()));
        return authUserVO;
    }
}
